package com.green.web.controller.manage;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.green.common.page.Pagination;
import com.green.model.manage.JoinPackage;
import com.green.model.manage.enums.JoinTypeEnum;
import com.green.model.manage.enums.StateEnum;
import com.green.service.common.ServiceContext;
import com.green.service.manage.AreaService;
import com.green.service.manage.JoinPackageService;
import com.green.web.controller.common.BaseController;

/**
 * 加盟套餐
 * @author 谷辉
 *
 */
@Controller
@RequestMapping(value = "/joinPackage/*")
public class JoinPackageController extends BaseController{

	@Resource
	private JoinPackageService joinPackageService;		

	@Resource
	private AreaService areaService;	
	
	@RequestMapping(value = "manager")
	public String manager(ModelMap modelMap) {
		modelMap.put("joinTypeEnum", JoinTypeEnum.toList());//套餐类型
		return "manage/JoinPackageManager";
	}
	/**
	 * @param modelMap套餐添加页面
	 * @return
	 */
	@RequestMapping(value = "add")
	public String add(ModelMap modelMap) {
		modelMap.put("joinTypeEnum", JoinTypeEnum.toList());//套餐类型
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("level", 2);
		modelMap.put("areaList", this.areaService.getAreaList(param));
		return "manage/JoinPackageAdd";
	}
	
	/**
	 * @param modelMap保存套餐设置
	 * @return
	 */
	@RequestMapping(value = "save")
	@ResponseBody
	public Map<String, Object> save(ModelMap modelMap,JoinPackage joinPackage) {
		joinPackage.setCreateBy(ServiceContext.getLoginPerson());
		joinPackage.setId(UUID.randomUUID().toString());	
		joinPackage.setStatus(StateEnum.DISABLED);
		int count =joinPackageService.insertJoinPackage(joinPackage);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("message", count);
		return result;
	}
	/**
	 * 查询套餐数据
	 * 
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	public void listData(Pagination<JoinPackage> page,HttpServletResponse response) {
		Map<String, Object> param = new HashMap<String, Object>();	
		param.put("joinType", getString("joinType"));
		page =joinPackageService.getJoinPackagePagination(page, param);
        
		outPrint(response, JSONObject.fromObject(page, getDefaultJsonConfig()));
	}
	/**
	 * 禁用 
	 * 启用
	 * @return
	 */
	@RequestMapping("updateJoinPackage")
	@ResponseBody
	public Map<String, Object> updateJoinPackage() {
		String id = getString("id");
		int type = getInt("type");
		int count = 0;
		if (null != id && !id.equals("")) {
			JoinPackage joinPackage=joinPackageService.getJoinPackageById(id);
			if(type==1){
				joinPackage.setStatus(StateEnum.DISABLED);
			}else{
				joinPackage.setStatus(StateEnum.EFFICTIVE);
			}
			
			joinPackage.setLastUpdateBy(ServiceContext.getLoginPerson());
			count=joinPackageService.updateJoinPackage(joinPackage);
		}
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("message", count);
		return result;
	}
}
